/* -+- file  ./kf2/GUI/ItemMenu.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Mono.Unix;
using Habazie.kf2.Core;
using Habazie.kf2.Exposition;
using Habazie.kf2.Net;
using Habazie.kf2.Roster;

namespace Habazie.kf2.GUI
{

	///<summary>
	///	Account context menu
	///</summary>
	public class AccountMenu : Gtk.Menu
	{
		private Account m_Account;
	
		
		public AccountMenu (Account a)
		{
			m_Account = a;
			
			Gtk.ImageMenuItem i;
			
			i = new Gtk.ImageMenuItem (Catalog.GetString ("Status"));
			StatusMenu menu = new StatusMenu ();
			menu.StatusActivated += OnStatusSelected;
			i.Submenu = menu;
			i.Show ();
			Append (i);
			
			i = new Gtk.ImageMenuItem (Catalog.GetString ("Modify Account"));
			i.Image = new Gtk.Image (Gtk.Stock.Properties, Gtk.IconSize.Menu);
			i.Activated += OnModify;
			i.Show ();
			Append (i);
			
			i = new Gtk.ImageMenuItem (Catalog.GetString ("Privacy"));
	//		i.Image = new Gtk.Image (Gtk.Stock.Properties, Gtk.IconSize.Menu);
			i.Activated += OnPrivacy;
			i.Sensitive = m_Account.IsAuthenticated;
			i.Sensitive = false;
			i.Show ();
			Append (i);
			
			i = new Gtk.ImageMenuItem (Catalog.GetString ("XML Console"));
			i.Image = new Gtk.Image (System.Reflection.Assembly.GetCallingAssembly (), "xml-console.png");
			i.Activated += OnXmlConsole;
			i.Show ();
			Append (i);
		}
		
		
		protected virtual void OnStatusSelected (object o, StatusEventArgs args)
		{
			m_Account.SetStatus (args.Presence, args.Status, args.Show);
		}
		
		protected virtual void OnModify (object o, EventArgs args)
		{
			AccountDialog win = new AccountDialog (m_Account);
			win.Show ();
		}
		
		
		protected virtual void OnPrivacy (object o, EventArgs args)
		{
			PrivacyListManager win = new PrivacyListManager (m_Account);
			win.Show ();
		}
		
		
		protected virtual void OnXmlConsole (object o, EventArgs args)
		{
			XmlConsole win = new XmlConsole (m_Account);
			win.Show ();
		}
	}

	public delegate void StatusEventHandler (object o, StatusEventArgs args);
	
	///<summary>
	///	A menu with statuses
	///</summary>
	public class StatusMenu : Gtk.Menu
	{
		///<summary>Status from the list has been selected</summary>
		public event StatusEventHandler StatusActivated = null;
		
		public StatusMenu ()
		{
			Gtk.ImageMenuItem i;	// TODO: translate + use Util.StatusToString
			
			i = new Gtk.ImageMenuItem ("Online");
			i.Name = "online";
			i.Activated += OnItemActivated;
			i.Image = new Gtk.Image (System.Reflection.Assembly.GetCallingAssembly (), "online.png");
			i.Show ();
			Append (i);
			
			i = new Gtk.ImageMenuItem ("Chat With Me");
			i.Name = "chat";
			i.Activated += OnItemActivated;
			i.Image = new Gtk.Image (System.Reflection.Assembly.GetCallingAssembly (), "chat-with-me.png");
			i.Show ();
			Append (i);
			
			i = new Gtk.ImageMenuItem ("Away");
			i.Name = "away";
			i.Activated += OnItemActivated;
			i.Image = new Gtk.Image (System.Reflection.Assembly.GetCallingAssembly (), "away.png");
			i.Show ();
			Append (i);
			
			i = new Gtk.ImageMenuItem ("Extended Away");
			i.Name = "xa";
			i.Activated += OnItemActivated;
			i.Image = new Gtk.Image (System.Reflection.Assembly.GetCallingAssembly (), "xa.png");
			i.Show ();
			Append (i);
			
			i = new Gtk.ImageMenuItem ("Do Not Disturb");
			i.Name = "dnd";
			i.Activated += OnItemActivated;
			i.Image = new Gtk.Image (System.Reflection.Assembly.GetCallingAssembly (), "dnd.png");
			i.Show ();
			Append (i);
			
			i = new Gtk.ImageMenuItem ("Offline");
			i.Name = "offline";
			i.Activated += OnItemActivated;
			i.Image = new Gtk.Image (System.Reflection.Assembly.GetCallingAssembly (), "offline.png");
			i.Show ();
			Append (i);
		}
		
		
		protected virtual void OnItemActivated (object o, EventArgs args)
		{
			Gtk.MenuItem item = (Gtk.MenuItem) o;
			jabber.protocol.client.PresenceType presence = jabber.protocol.client.PresenceType.available;
			string show = null;
			
			switch (item.Name) {
				case "online":
					break;
				case "chat":
					show = "chat";
					break;
				case "away":
					show = "away";
					break;
				case "xa":
					show = "xa";
					break;
				case "dnd":
					show = "dnd";
					break;
				case "offline":
					presence = jabber.protocol.client.PresenceType.unavailable;
					break;
				default:
					Util.Debug ("GUI", "Unknown status action");
					return;
			}
			
			if (StatusActivated != null)
				StatusActivated (this, new StatusEventArgs (presence, show, null));
		}
		
	}
	
	public class StatusEventArgs : EventArgs
	{
		private jabber.protocol.client.PresenceType m_Presence;
		private string m_Show = null;
		private string m_Status = null;
		
		public StatusEventArgs (jabber.protocol.client.PresenceType p,
		                        string show,
		                        string status)
		{
			m_Presence = p;
			m_Show = show;
			m_Status = status;
		}
		
		public jabber.protocol.client.PresenceType Presence {
			get { return m_Presence; }
			set { m_Presence = value; }
		}
		
		
		public string Show {
			get { return m_Show; }
			set { m_Show = value; }
		}
		
		public string Status {
			get { return m_Status; }
			set { m_Status = value; }
		}
	}
}
